/*
 *
 *
 */

#include "gkt_language.h"
#include "gkt_text.h"

#if defined(GKT_CONFIG_SCREEN_ENABLE) && GKT_CONFIG_SCREEN_ENABLE

typedef struct {
	const char *const	*text_table;
	uint32_t	table_size;
} text_attr_s;
static text_attr_s s_text_attrs[GKT_LANG_MAX];
static uint32_t s_text_ids[GKT_TEXT_ID_TYPE_MAX];

void gkt_text_set_table(gkt_language_e language, 
			const char *const text_table[], 
			uint32_t table_size)
{
	if ((language < GKT_LANG_MAX)
		&& text_table && (table_size > 0)) 
	{
		s_text_attrs[language].text_table = text_table;
		s_text_attrs[language].table_size = table_size;
	}
}

void gkt_text_set_id(uint32_t type, uint32_t id)
{
	if (type < GKT_TEXT_ID_TYPE_MAX)
		s_text_ids[type] = id;
}

const char *gkt_text_get_ptr(uint32_t text_id)
{
	uint32_t index = text_id & (~GKT_TEXT_ID_MASK);
	const char *text;
	int lang_idx;

	if (text_id < s_text_ids[GKT_TEXT_ID_MAX]) {
		if (index < s_text_attrs[GKT_LAGNUAGE].table_size) {
			text = s_text_attrs[GKT_LAGNUAGE].text_table[index];
			if (__LIKELY(text) || (text_id < s_text_ids[GKT_TEXT_ID_NECESSARY]))
				return text;
		}

		if (text_id > s_text_ids[GKT_TEXT_ID_NECESSARY]) {
			lang_idx = 0;
			while ((lang_idx != GKT_LAGNUAGE) && (lang_idx < GKT_LANG_MAX)) {
				if (index < s_text_attrs[lang_idx].table_size) {
					text = s_text_attrs[lang_idx].text_table[index];
					if (__LIKELY(text))
						return text;
				}
			}
		}
	}

	return NULL;		
}

#endif

